import 'package:dio/dio.dart';
import 'package:get/get.dart';
import 'package:study_demo/api/bean.dart';
import 'package:study_demo/base/BaseController.dart';
import 'package:study_demo/util/utils.dart';

import '../../../api/api.dart';

class IpQueryController extends BaseController {
  final result = ''.obs;
  IpInfo ipInfo = IpInfo();

  @override
  onInit() {
    super.onInit();
    query();
  }

  query() async {
    Dio dio = Dio();
    var response = await dio.request(ipQueryUrl);
    if (response.statusCode == 200) {
      ipInfo = IpInfo.fromJson(response.data['data']);
      String ip = response.data['ip'];
      ipInfo.ip = ip;
      result.value = 'ip：' +
          ip +
          '\n归属地：' +
          ipInfo.country! +
          ' ' +
          ipInfo.prov! +
          ' ' +
          ipInfo.city! +
          ' ' +
          ipInfo.district! +
          '\n邮编：' +
          ipInfo.adcode!;
    }
  }

  copy() {
    copyToClipboard(ipInfo.ip!);
  }
}
